package com.mes.bac.backend.common.vo;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;

import java.io.Serializable;

@Data
@Schema(description = "统一API响应结果封装")
public class R<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    @Schema(description = "状态码, 200表示成功", requiredMode = Schema.RequiredMode.REQUIRED, example = "200")
    private Integer code;

    @Schema(description = "响应消息", requiredMode = Schema.RequiredMode.REQUIRED, example = "操作成功")
    private String message;

    @Schema(description = "响应数据")
    private T data;

    // 构造方法私有化
    private R() {}

    // --- 成功的静态工厂方法 ---

    public static <T> R<T> success() {
        R<T> r = new R<>();
        r.setCode(200);
        r.setMessage("操作成功");
        r.setData(null);
        return r;
    }

    public static <T> R<T> success(T data) {
        R<T> r = new R<>();
        r.setCode(200);
        r.setMessage("操作成功");
        r.setData(data);
        return r;
    }

    /**
     * 成功，并返回自定义消息 (无数据)
     * @param message 自定义消息
     * @return R
     */
    public static <T> R<T> successMessage(String message) {
        R<T> r = new R<>();
        r.setCode(200);
        r.setMessage(message);
        r.setData(null);
        return r;
    }

    public static <T> R<T> success(String message, T data) {
        R<T> r = new R<>();
        r.setCode(200);
        r.setMessage(message);
        r.setData(data);
        return r;
    }

    // --- 失败的静态工厂方法 ---

    public static <T> R<T> fail() {
        R<T> r = new R<>();
        r.setCode(500); // 默认失败码
        r.setMessage("操作失败");
        r.setData(null);
        return r;
    }

    public static <T> R<T> fail(String message) {
        R<T> r = new R<>();
        r.setCode(500);
        r.setMessage(message);
        r.setData(null);
        return r;
    }

    public static <T> R<T> fail(Integer code, String message) {
        R<T> r = new R<>();
        r.setCode(code);
        r.setMessage(message);
        r.setData(null);
        return r;
    }
}